package com.rongshu.sendmessage.util.httpUtil;

import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.bean.UpDataVersionBean;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.upData.UpDataVersionActivity;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2017/7/2 0002.
 */

public interface IModel extends Constant  {

    public void cancelAllRequest(String... tag);

    public class AjaxResult implements Serializable {
        private JSONObject jsonObj;

        public AjaxResult(String json) {
            init(json);
            checkVerSionData();

        }
        //如果有跳转或者关闭页面则不判断是否更新版本
        public AjaxResult(String json,Boolean isCheck) {
            init(json);
            if (isCheck)
                checkVerSionData();
        }

        private void init(String json) {
            this.jsonObj = JSON.parseObject(json);
            //调用获取本渠道是否需要显示广告
//            getappChannelData();
            //储存json中的每个人的id分VirtualDeviceNumber
            try {
                Log.e("resultData:",jsonObj.toJSONString());
                MyApplication.editor.putString(HYContent.VirtualDeviceNumber, jsonObj.getString("virtualDeviceNumber"));
                MyApplication.editor.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public int getCode() {
            String s = jsonObj.getString("status");
            return s != null && s.equals("success") ? SUCCESS_CODE : ERROR_CODE;
        }

        public JSONObject getJsonObj() {
            return jsonObj;
        }

        public String getMsg() {
            return jsonObj.getString("massage");
        }

        public String getObj() {
            return jsonObj.getString("obj");
        }

        public JSONObject getData() {
            return jsonObj.getJSONObject("obj");
        }

        public JSONArray getJSONArrayData() {
            return jsonObj.getJSONArray("obj");
        }

        public String getKey() {
            return jsonObj.getString("key");
        }

        public String getStatus() {
            return jsonObj.getString("status");
        }

        public String getString(String key) {
            return jsonObj.getString(key);
        }

        public int getInt(String key) {
            return jsonObj.getIntValue(key);
        }

        public AdvertisingDisCon getAdvertisingDisCon() {

            List<AdvertisingDisCon> advertisingDisCons = JSON.parseArray(JSON.toJSONString(jsonObj.get("advertisingDisCon")), AdvertisingDisCon.class);
            AdvertisingDisCon advertisingDisCon;
            if (advertisingDisCons==null||advertisingDisCons.size()==0){
                advertisingDisCon=new AdvertisingDisCon();
            }else{
                advertisingDisCon=advertisingDisCons.get(0);
            }
            return advertisingDisCon;
        }
        public List<AdvertisingDisCon> getAdvertisingDisCons() {
            List<AdvertisingDisCon> advertisingDisCons = JSON.parseArray(JSON.toJSONString(jsonObj.get("advertisingDisCon")), AdvertisingDisCon.class);
            return advertisingDisCons;
        }
        public UpDataVersionBean getVersionData(){
            UpDataVersionBean versionData = JSON.parseObject(JSON.toJSONString(jsonObj.getJSONObject("versionData")), UpDataVersionBean.class);
            return versionData;
        }
        public void checkVerSionData() {
            UpDataVersionBean versionData=getVersionData();
            if (versionData==null){
                versionData=new UpDataVersionBean();
            }
            if (versionData.isUpdate()){
                if (StringUtils.isEmpty(versionData.getAppH5Index())){
                    return;
                }
                Intent intent=new Intent(MyApplication.instance, UpDataVersionActivity.class);
                intent.putExtra("upData",versionData);//"upDate"
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MyApplication.instance.startActivity(intent);
            }

        }

        public List<AppChannelData> getappChannelData() {
            List<AppChannelData> appChannelData = JSON.parseArray(JSON.toJSONString(jsonObj.getJSONArray("appChannelData")), AppChannelData.class);
            String channel = MyApplication.mSettings.getString("channel", "");
            boolean tenmShow = false;
            if (appChannelData!=null)
                for (AppChannelData data : appChannelData) {
                    if (!StringUtils.isEmpty(data.getAppStorekeyMark()) && data.getAppStorekeyMark().equals(channel)) {
                        tenmShow = data.isShowAdvertisement();
                    }
                }
            MyApplication.editor.putBoolean(HYContent.IS_SHOW, tenmShow);
            MyApplication.editor.commit();
            return appChannelData;
        }

        public static class AppChannelData {
            private String appStorekeyMark;
            private boolean showAdvertisement;

            public String getAppStorekeyMark() {
                return appStorekeyMark;
            }

            public void setAppStorekeyMark(String appStorekeyMark) {
                this.appStorekeyMark = appStorekeyMark;
            }

            public boolean isShowAdvertisement() {
                return showAdvertisement;
            }

            public void setShowAdvertisement(boolean showAdvertisement) {
                this.showAdvertisement = showAdvertisement;
            }
        }


        //        广告的
        public static class AdvertisingDisCon {
            private String advertisementChannel;
            private String advertisementType;
            private String advertisementTypeStr;
            private String advertisementId;
            private boolean isShow;

            public String getAdvertisementChannel() {
                return advertisementChannel;
            }

            public void setAdvertisementChannel(String advertisementChannel) {
                this.advertisementChannel = advertisementChannel;
            }

            public String getAdvertisementType() {
                return advertisementType;
            }

            public void setAdvertisementType(String advertisementType) {
                this.advertisementType = advertisementType;
            }

            public String getAdvertisementTypeStr() {
                return advertisementTypeStr;
            }

            public void setAdvertisementTypeStr(String advertisementTypeStr) {
                this.advertisementTypeStr = advertisementTypeStr;
            }

            public String getAdvertisementId() {
                return advertisementId;
            }

            public void setAdvertisementId(String advertisementId) {
                this.advertisementId = advertisementId;
            }

            public boolean isShow() {
                return isShow;
            }

            public void setShow(boolean show) {
                isShow = show;
            }
        }
    }

    public static interface OnModelCallbackListener {
        public void onResult(Object o, int code, String msg);
    }
}
